//
//  FreshProductViewController.m
//  Youhuiju
//
//  Created by yutenys on 16/8/1.
//  Copyright © 2016年 yutenys. All rights reserved.
//

#import "FreshProductViewController.h"
//跳转界面
#import "GoodsDetailViewController.h"

//自定义视图
#import "FreshProductTopView.h"




//自定义cell
#import "FreshProductCollectionViewCell.h"
#import "FreshProductCollectionReusableView.h"
@interface FreshProductViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong)FreshProductTopView *topView;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property(nonatomic,strong)UIButton *toTopButton;//回顶部

//占位数据
@property(nonatomic,strong)NSArray *dataSourceArray;

@end



@implementation FreshProductViewController

static NSString *FreshProductCollectionViewCellIden = @"FreshProductCollectionViewCellIden";
static NSString *FreshProductHeaderView = @"FreshProductHeaderView";


- (void)viewDidLoad {
    [super viewDidLoad];

    [self load_UI];
}


#pragma mark ------ load_UI
-(void)load_UI
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self topView];
    
    self.dataSourceArray = @[@"4月15日  16:00更新",@"4月14日  16:00更新",@"4月13日  16:00更新",@"4月12日  16:00更新"];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource  = self;
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"FreshProductCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:FreshProductCollectionViewCellIden];
    [self.collectionView registerClass:[FreshProductCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:FreshProductHeaderView];
    
    _toTopButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _toTopButton.backgroundColor = [UIColor clearColor];
    [_toTopButton setBackgroundImage:LoadImage(@"toTop") forState:UIControlStateNormal];
    [_toTopButton addTarget:self action:@selector(toTopAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_toTopButton];
    [_toTopButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).offset(-20 * FitW);
        make.bottom.equalTo(self.view.mas_bottom).offset(-100 * FitH);
        make.size.mas_equalTo(CGSizeMake(40 * FitW, 40 * FitH));
    }];
    _toTopButton.alpha = 0;
    
}

#pragma mark ------- collectionView delagate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 4;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 10;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(170 * FitW, 240 * FitH);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 12 * FitW, 0, 12 * FitW);
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
  
    return CGSizeMake(self.collectionView.frame.size.width, 47 * FitH);
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableView = nil;
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        
        FreshProductCollectionReusableView * header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:FreshProductHeaderView forIndexPath:indexPath];
        header.backgroundColor = [UIColor colorWithHexString:@"F5F5F5"];
        //有缓存了
        //        [header.imgV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kDataServer2,_topic_img_url]] placeholderImage:[UIImage imageNamed:@"AppIcon60x60@3x"]];

            header.sectionLabel.text = self.dataSourceArray[indexPath.section];

        reusableView = header;
    }
    
    
    return reusableView;

}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FreshProductCollectionViewCell   *cell = [collectionView dequeueReusableCellWithReuseIdentifier:FreshProductCollectionViewCellIden forIndexPath:indexPath];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    GoodsDetailViewController  *DVC = [[GoodsDetailViewController alloc]initWithNibName:@"GoodsDetailViewController" bundle:nil];
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:DVC animated:YES];
//    self.hidesBottomBarWhenPushed = NO;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    WeakObj(self);
    
    if (scrollView.contentOffset.y > 700 * FitH)
    {
        [UIView animateWithDuration:1 animations:^{
            weakself.toTopButton.alpha = 1;
        }];
    }
    else
    {
        [UIView animateWithDuration:1 animations:^{
            weakself.toTopButton.alpha = 0;
        }];
    }
}
-(void)toTopAction:(UIButton *)sender
{
    DLog(@"回顶部");
    [self.collectionView setContentOffset:CGPointMake(0, 0) animated:YES];
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear: YES];
//    self.tabBarController.tabBar.hidden = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark ----- lazy loading
-(FreshProductTopView *)topView
{
    WeakObj(self);
    if (!_topView)
    {
        _topView = [[FreshProductTopView alloc]init];
        _topView.titleLabel.text = weakself.titleString;
        [self.view addSubview:_topView];
        [_topView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view.mas_left);
            make.right.equalTo(self.view.mas_right);
            make.top.equalTo(self.view.mas_top).offset(20);
            make.height.mas_offset(44);
        }];
        
        _topView.backButtonClickedHandler = ^{
        
            [weakself.navigationController popViewControllerAnimated:YES];
        };
    }
    return _topView;
}

@end
